//
//  UIViewController+Create.m
//  mmbang
//
//  Created by 杨世昌 on 14/11/24.
//  Copyright (c) 2014年 iyaya. All rights reserved.
//

#import "UIViewController+Create.h"

@implementation UIViewController (Create)

+ (NSString *)getXibNameWithClassName:(NSString *)className {
    Device_Screen_Type screenType = [UIDevice getDeviceScreenType];
    
    NSString *suffix = @"";
    
    switch (screenType){
        case iPad:{
            suffix = suffix_iPad;
            break;
        }
        case iPhone_Screen_5_5:{
            suffix = suffix_iPhone_5_5;
            break;
        }
        case iPhone_Screen_4_7:{
            suffix = suffix_iPhone_4_7;
            break;
        }
        case iPhone_Screen_4:{
            suffix = suffix_iPhone_4;
            break;
        }
        case iPhone_Screen_3_5:{
            suffix = suffix_iPhone_3_5;
            break;
        }
        default:
            suffix = suffix_None;
            break;
    }
    
    return [NSString stringWithFormat:@"%@%@",className,suffix];
}


+ (instancetype)createWithNibName:(NSString *)nibNameOrNil {
    Class selfClass = [self class];
    id vc = [[selfClass alloc] initWithNibName:nibNameOrNil bundle:nil];
    return vc;
}

+ (instancetype)createWithNib {
    NSString *className = NSStringFromClass([self class]);
    
    NSString *xibName = [self getXibNameWithClassName:className];
    
    // 判断 是否 使用了 特别命名的 xib，如果没有，则使用系统默认的className
    NSString *nibName = [NSString stringWithFormat:@"%@.nib",xibName];
    NSString *xibPath =  [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:nibName];
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:xibPath]){
        DLog(@"规范xib文件存在");
        return [self createWithNibName:xibName];
    }else{
        return [self createWithNibName:className];
    }
}

@end
